import { Select } from 'antd';
import type { SelectProps } from 'antd';
import { useState } from 'react';
import { medicineSearch } from '@/services/medicine'

interface UserSearchProps {
    value?: string;
    onChange?: (value: string) => void;
}

const MedicineSearch: React.FC<UserSearchProps> = ({ value, onChange }) => {

    const [data, setData] = useState<SelectProps['options']>([]);
    const handleSearch = async (newValue: string) => {
        if (newValue) {
            const { dataList } = await medicineSearch({ name: newValue });
            const result = dataList.map((item) => {
                return {
                    value: item.name,
                    text: item.name,
                }
            })
            setData(result)
        } else {
            setData([]);
        }
    };

    return (<Select showSearch
        value={value}
        style={{ width: 300 }}
        onChange={onChange}
        placeholder='不填写默认导出全部'
        defaultActiveFirstOption={false}
        suffixIcon={null}
        filterOption={false}
        onSearch={handleSearch}
        notFoundContent={null}
        options={(data || []).map((d) => ({
            value: d.value,
            label: d.text,
        }))} />)
}
export default MedicineSearch;